//
//  ViewController.swift
//  LayerPrac
//
//  Created by flowerflower on 2019/8/28.
//  Copyright © 2019 cba023. All rights reserved.
//

import UIKit

class ViewController: UIViewController {

    let drawLayer = CALayer()
    
    override func viewDidLoad() {
        super.viewDidLoad()
        self.view.layer.addSublayer(self.drawLayer)
        self.drawLayer.contentsScale = UIScreen.main.scale
        self.drawLayer.borderColor = UIColor.cyan.cgColor
        self.drawLayer.borderWidth = 5.0;
        self.drawLayer.delegate = self;
        
    }
    
    override func viewDidLayoutSubviews() {
        super.viewDidLayoutSubviews()
        let wh = self.view.frame.width - 40.0
        self.drawLayer.frame = CGRect(x: 20, y: 100, width: wh, height: wh)
    }
    
    override func viewDidAppear(_ animated: Bool) {
        super.viewDidAppear(animated)
        // layer进行回执
        self.drawLayer.setNeedsDisplay()
    }
    
    override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
        if touches.first?.view == self.view {
            let vc = LayerProgressViewController()
            self.navigationController?.pushViewController(vc, animated: true)
        }
    }
}

extension ViewController : CALayerDelegate {
    func draw(_ layer: CALayer, in ctx: CGContext) {
        // 画笔线颜色
        ctx.setStrokeColor(UIColor.red.cgColor)
        ctx.setFillColor(UIColor.red.cgColor)
        ctx.setLineWidth(2.0)
        ctx.addEllipse(in: CGRect(x: 10, y: 10, width: 50, height: 50))
        // 绘制渲染
        ctx.fillPath()
    }
}

